package org.lanyu.springainovel.common.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.lanyu.springainovel.common.entity.TaskNotification;

import java.util.List;

/**
 * 任务通知Mapper接口
 */
@Mapper
public interface TaskNotificationMapper extends BaseMapper<TaskNotification> {

    /**
     * 根据用户ID获取未读通知数量
     * @param userId 用户ID
     * @return 未读通知数量
     */
    @Select("SELECT COUNT(*) FROM task_notification WHERE user_id = #{userId} AND is_read = false AND is_deleted = false")
    int countUnreadByUserId(@Param("userId") Long userId);

    /**
     * 根据用户ID获取未读任务通知数量
     * @param userId 用户ID
     * @return 未读任务通知数量
     */
    @Select("SELECT COUNT(*) FROM task_notification WHERE user_id = #{userId} AND is_read = false AND is_task = true AND is_deleted = false")
    int countUnreadTasksByUserId(@Param("userId") Long userId);

    /**
     * 根据用户ID获取所有通知（按创建时间倒序）
     * @param userId 用户ID
     * @return 通知列表
     */
    @Select("SELECT * FROM task_notification WHERE user_id = #{userId} AND is_deleted = false ORDER BY created_at DESC")
    List<TaskNotification> findAllByUserId(@Param("userId") Long userId);

    /**
     * 根据用户ID获取未读通知（按创建时间倒序）
     * @param userId 用户ID
     * @return 未读通知列表
     */
    @Select("SELECT * FROM task_notification WHERE user_id = #{userId} AND is_read = false AND is_deleted = false ORDER BY created_at DESC")
    List<TaskNotification> findUnreadByUserId(@Param("userId") Long userId);

    /**
     * 根据用户ID获取活动任务
     * @param userId 用户ID
     * @return 活动任务列表
     */
    @Select("SELECT * FROM task_notification WHERE user_id = #{userId} AND is_task = true AND status IN ('PENDING', 'RUNNING') AND is_deleted = false ORDER BY created_at DESC")
    List<TaskNotification> findActiveTasksByUserId(@Param("userId") Long userId);

    /**
     * 根据用户ID获取已完成任务
     * @param userId 用户ID
     * @return 已完成任务列表
     */
    @Select("SELECT * FROM task_notification WHERE user_id = #{userId} AND is_task = true AND status IN ('COMPLETED', 'FAILED', 'CANCELLED') AND is_deleted = false ORDER BY completed_at DESC")
    List<TaskNotification> findCompletedTasksByUserId(@Param("userId") Long userId);

    /**
     * 根据任务ID获取任务通知
     * @param taskId 任务ID
     * @return 任务通知
     */
    @Select("SELECT * FROM task_notification WHERE task_id = #{taskId} AND is_deleted = false")
    TaskNotification findByTaskId(@Param("taskId") String taskId);

    /**
     * 根据用户ID和任务类型获取活动任务
     * @param userId 用户ID
     * @param taskType 任务类型
     * @return 活动任务列表
     */
    @Select("SELECT * FROM task_notification WHERE user_id = #{userId} AND task_type = #{taskType} AND status IN ('PENDING', 'RUNNING') AND is_deleted = false ORDER BY created_at DESC")
    List<TaskNotification> findActiveTasksByUserIdAndType(@Param("userId") Long userId, @Param("taskType") String taskType);

    /**
     * 标记通知为已读
     * @param id 通知ID
     * @return 影响行数
     */
    @Update("UPDATE task_notification SET is_read = true, updated_at = NOW() WHERE id = #{id}")
    int markAsRead(@Param("id") Long id);

    /**
     * 标记用户所有通知为已读
     * @param userId 用户ID
     * @return 影响行数
     */
    @Update("UPDATE task_notification SET is_read = true, updated_at = NOW() WHERE user_id = #{userId} AND is_read = false")
    int markAllAsReadByUserId(@Param("userId") Long userId);

    /**
     * 删除已读的任务通知
     * @param userId 用户ID
     * @return 影响行数
     */
    @Update("UPDATE task_notification SET is_deleted = true, updated_at = NOW() WHERE user_id = #{userId} AND is_task = true AND is_read = true")
    int deleteReadTasksByUserId(@Param("userId") Long userId);

    /**
     * 删除已读的通知
     * @param userId 用户ID
     * @return 影响行数
     */
    @Update("UPDATE task_notification SET is_deleted = true, updated_at = NOW() WHERE user_id = #{userId} AND is_read = true")
    int deleteReadNotificationsByUserId(@Param("userId") Long userId);

    /**
     * 删除指定天数前的已完成任务
     * @param userId 用户ID
     * @param days 天数
     * @return 影响行数
     */
    @Update("UPDATE task_notification SET is_deleted = true, updated_at = NOW() WHERE user_id = #{userId} AND is_task = true AND status IN ('COMPLETED', 'FAILED', 'CANCELLED') AND completed_at < DATE_SUB(NOW(), INTERVAL #{days} DAY)")
    int deleteOldCompletedTasks(@Param("userId") Long userId, @Param("days") int days);
}